package userDefThreadPool;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

class ThreadPool {
    private BlockingQueue<Runnable> taskQueue;

    public ThreadPool(int numberOfThreads) {
        taskQueue = new LinkedBlockingQueue<Runnable>(10);

        for (int i = 0; i < numberOfThreads; i++) {
			new PoolThread(taskQueue).start();
        }
    }

    public void execute(Runnable task) throws InterruptedException {
        taskQueue.put(task);
    }
}


class PoolThread extends Thread {
    private BlockingQueue<Runnable> taskQueue;

    public PoolThread(BlockingQueue<Runnable> queue) {
        taskQueue = queue;
    }

    public void run() {
        while (true) {
            try {
                taskQueue.take().run();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}